Send a file with multipart/form-data to OctoPrint REST API

That isn't 100% true. The HTTP In node does support file uploads now - select Post and tick the box to enable file uploads.

To send a multipart upload from the HTTP Request node is more work, but doable.

Here's a Function that will construct a suitable message to pass to the HTTP Request node:

msg.headers = {
    "Content-Type": "multipart/form-data; boundary=------------------------d74496d66958873e"
}


msg.payload = '--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="select"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="print"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="file"; filename="whistle_v2.gcode"\r\n'+
'Content-Type: application/octet-stream\r\n'+
'\r\n'+
'contents of the file\r\n'+
'--------------------------d74496d66958873e--\r\n';


return msg;

I've based the content from the example request you shared earlier.

A couple key points

  • you need to use \r\n for newlines
  • the boundary string set in the header defines how the separate form parts are separated in the body. When it appears in the body, it has two extra -- in front. The very last one also has -- added to the end. (Took quite a bit of googling and examing curl trace to spot that)

Hopefully from that you can insert the contents of the file you want to upload at the appropriate point.